package com.jzr.pda.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.pda.db1.model.TDoctorOrder;
import com.jzr.pda.db1.service.TDoctorOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "医嘱接口")
@RestController
@RequestMapping("/api/doctorOrder")
public class DoctorOrderController {
    @Autowired
    private TDoctorOrderService doctorOrderService;

    @ApiOperation("病人医嘱列表")
    @GetMapping("/list")
    public Result<List<TDoctorOrder>> list(@ApiParam("病人编码") @RequestParam String patientCode,
                                           @ApiParam("住院号") @RequestParam String patientCureNo){
        if(U.isBlank(patientCode)){
            return Result.fail("病人编码patientCode 不能为空");
        }
        if(U.isBlank(patientCureNo)){
            return Result.fail("住院号patientCureNo 不能为空");
        }
        QueryWrapper<TDoctorOrder> qw = new QueryWrapper<>();
        qw.orderByAsc("group_code");
        qw.eq(TDoctorOrder.PatientCode_,patientCode)
                .eq(TDoctorOrder.PatientCureNo_,patientCureNo);
        List<TDoctorOrder> list = doctorOrderService.list(qw);
        return Result.success("成功",list,list.size());
    }

    private boolean isCancel(List<TDoctorOrder> list){
        for(TDoctorOrder doctorOrder : list){
            Boolean isCancel = doctorOrder.getIsCancel();
            if(isCancel != null ){
                return isCancel;
            }
        }
        return false;
    }

    @ApiOperation(value="执行医嘱",consumes="application/x-www-form-urlencoded")
    @PostMapping("/exec")
    public Result<Boolean> exec(@ApiParam("医嘱编码") String orderNo){
        if(U.isBlank(orderNo)){
            return Result.fail("医嘱编码不能为空");
        }
        QueryWrapper<TDoctorOrder> qw = new QueryWrapper<>();
        qw.eq(TDoctorOrder.OrderNo_,orderNo);
        List<TDoctorOrder> list = doctorOrderService.list(qw);
        if(list == null || list.size() <= 0){
            return Result.fail("医嘱编码不存在");
        }
        if(isCancel(list)){
            return Result.fail("该医嘱已经被取消");
        }
        int count = doctorOrderService.exec(orderNo);
        if(count <= 0){
            return Result.fail("已经被执行，不能重复执行");
        }
        return Result.success("成功",true);
    }

    @ApiOperation(value="执行医嘱",consumes="application/x-www-form-urlencoded")
    @PostMapping("/cancelExec")
    public Result<Boolean> cancelExec(@ApiParam("医嘱编码") String orderNo){
        if(U.isBlank(orderNo)){
            return Result.fail("医嘱编码不能为空");
        }
        QueryWrapper<TDoctorOrder> qw = new QueryWrapper<>();
        qw.eq(TDoctorOrder.OrderNo_,orderNo);
        List<TDoctorOrder> list = doctorOrderService.list(qw);
        if(list == null || list.size() <= 0){
            return Result.fail("医嘱编码不存在");
        }
        if(isCancel(list)){
            return Result.fail("该医嘱已经被取消");
        }
        int count = doctorOrderService.cancelExec(orderNo);
        if(count <= 0){
            return Result.fail("已经被取消执行，不能重复取消执行");
        }
        return Result.success("成功",true);
    }
}
